<?php
// 设置 HTTP 头为 XML
header("Content-Type: text/xml");

$serviceUrl = "http://yourdomain.com/soap-server.php";  // 服务端 URL
$namespace = "http://yourdomain.com/soap";  // 服务的命名空间

// 定义服务类和方法映射
$class = "MyService";
$methods = [
    'add' => ['a' => 'int', 'b' => 'int', 'result' => 'int'],
    'multiply' => ['a' => 'int', 'b' => 'int', 'result' => 'int']
];

// 动态生成 WSDL 内容
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
             xmlns:tns="<?= $namespace ?>"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             name="<?= $class ?>"
             targetNamespace="<?= $namespace ?>">

    <types>
        <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="<?= $namespace ?>">
            <?php foreach ($methods as $method => $params): ?>
                <element name="<?= $method ?>Request">
                    <complexType>
                        <sequence>
                            <?php foreach ($params as $param => $type): ?>
                                <?php if ($param !== 'result'): ?>
                                    <element name="<?= $param ?>" type="xsd:<?= $type ?>"/>
                                <?php endif; ?>
                            <?php endforeach; ?>
                        </sequence>
                    </complexType>
                </element>
                <element name="<?= $method ?>Response">
                    <complexType>
                        <sequence>
                            <element name="result" type="xsd:<?= $params['result'] ?>"/>
                        </sequence>
                    </complexType>
                </element>
            <?php endforeach; ?>
        </schema>
    </types>

    <?php foreach ($methods as $method => $params): ?>
        <message name="<?= $method ?>Request">
            <part name="parameters" element="tns:<?= $method ?>Request"/>
        </message>
        <message name="<?= $method ?>Response">
            <part name="parameters" element="tns:<?= $method ?>Response"/>
        </message>
    <?php endforeach; ?>

    <portType name="<?= $class ?>PortType">
        <?php foreach ($methods as $method => $params): ?>
            <operation name="<?= $method ?>">
                <input message="tns:<?= $method ?>Request"/>
                <output message="tns:<?= $method ?>Response"/>
            </operation>
        <?php endforeach; ?>
    </portType>

    <binding name="<?= $class ?>Binding" type="tns:<?= $class ?>PortType">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <?php foreach ($methods as $method => $params): ?>
            <operation name="<?= $method ?>">
                <soap:operation soapAction="<?= $namespace ?>#<?= $method ?>"/>
                <input>
                    <soap:body use="literal"/>
                </input>
                <output>
                    <soap:body use="literal"/>
                </output>
            </operation>
        <?php endforeach; ?>
    </binding>

    <service name="<?= $class ?>Service">
        <port name="<?= $class ?>Port" binding="tns:<?= $class ?>Binding">
            <soap:address location="<?= $serviceUrl ?>"/>
        </port>
    </service>
</definitions>